/**
 * 文章管理管理初始化
 */
var Article = {
    id: "ArticleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Article.initColumn = function () {
    return [
            {field: 'selectItem',   checkbox: true,  visible: true },
            {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '文章标题', field: 'title', visible: true, align: 'center', valign: 'middle'},
            {title: '用户ID', field: 'userId', visible: false, align: 'center', valign: 'middle'},
            {title: '文章封面图片', field: 'coverImage', visible: false, align: 'center', valign: 'middle'},
            {title: '文章专属二维码地址', field: 'qrcodePath', visible: false, align: 'center', valign: 'middle'},
            {title: '', field: 'isMarkdown', visible: false, align: 'center', valign: 'middle'},
            {title: '文章内容', field: 'content', visible: false, align: 'center', valign: 'middle'},
            {title: 'markdown版的文章内容', field: 'contentMd', visible: false, align: 'center', valign: 'middle'},
            {title: '是否置顶', field: 'top', visible: true, align: 'false', valign: 'middle'},
            {title: '类型', field: 'typeId', visible: true, align: 'center', valign: 'middle'},
            {title: '是否删除 1：启用 0:不启用', field: 'status', visible: false, align: 'center', valign: 'middle'},
            {title: '是否推荐', field: 'recommended', visible: false, align: 'center', valign: 'middle'},
            {title: '是否原创', field: 'original', visible: false, align: 'center', valign: 'middle'},
            {title: '文章简介，最多200字', field: 'description', visible: false, align: 'center', valign: 'middle'},
            {title: '文章关键字，优化搜索', field: 'keywords', visible: false, align: 'center', valign: 'middle'},
            {title: '是否开启评论', field: 'comment', visible: false, align: 'center', valign: 'middle'},
            {title: '发布时间', field: 'createTime', visible: false, align: 'center', valign: 'middle'},
            {title: '更新时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle'},
            {title: '浏览', field: 'lookCount', visible: true, align: 'center', valign: 'middle'},
            {title: '评论', field: 'commentCount', visible: true, align: 'center', valign: 'middle'},
            {title: '喜欢', field: 'loveCount', visible: true, align: 'center', valign: 'middle'},
            {
                title: '操作',
                field: 'operate',
                align: 'center',
                valign: 'middle',
                width:270,
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
    ];
};

/**
 * 检查是否选中
 */
Article.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Article.seItem = selected;
        var attr = [];
        for(var i=0;i<selected.length;i++){
            attr.push(selected[i].id);
        }
        Article.seItemId = attr;
        return true;
    }
};

/**
 * 取消选中编辑或者删除按钮那一行
 */
Article.cancelClick = function (id) {
    var checkBox = $("a[data-id='"+id+"']").closest("tr").find("td").eq(0).find("input");
    if(checkBox.is(':checked')){
        checkBox.prop('checked', false);
    }else{
        checkBox.prop('checked', true);
    }
};


/**
 * 发布文章添加文章管理
 */
Article.openAddArticle = function () {
    var width = $(window).width()*0.98+"px";
    var height = $(window).height()*0.98+"px";

    var index = layer.open({
        type: 2,
        title: '添加文章管理',
        area: [width, height], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/article/article_publish_md'
    });
    this.layerIndex = index;
};


/**
 * 打开查看文章管理详情
 */
Article.openArticleDetail = function (id,is_markdown) {
    var width = $(window).width()*0.98+"px";
    var height = $(window).height()*0.98+"px";
    Article.cancelClick(id);
    var index = layer.open({
        type: 2,
        title: '文章管理详情',
        area: [width, height], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/article/article/article_publish?id=' + id+'&is_markdown='+is_markdown
    });
    this.layerIndex = index;
};

/**
 * 删除文章管理
 */
Article.delete = function (id) {
    Article.cancelClick(id);
     var operation = function(){
         var ids = new Array();
         var ajax = new $ax(Feng.ctxPath + "/article/article/delete", function (data) {
             Feng.pass(data,function () {
                 Feng.success("删除成功!");
                 Article.table.refresh();
             });
         }, function (data) {
             Feng.error("删除失败!" + data.msg + "!");
         });
         ids.push(id);
         ajax.set("ids", ids);
         ajax.start();
     }
    Feng.confirm("确认删除?", operation);
};

/**
 * 删除文章评论
 */
Article.deleteMore = function () {
    if (this.check()) {
        var operation = function(){
            var ids = Article.seItemId;
            var ajax = new $ax(Feng.ctxPath + "/article/article/delete", function (data) {
                Feng.pass(data,function () {
                    Feng.success("删除成功!");
                    Article.table.refresh();
                });
            }, function (data) {
                Feng.error("删除失败!" + data.msg + "!");
            });
            ajax.set("ids", ids);
            ajax.start();
        }
        Feng.confirm("确认删除?", operation);
    }

};

/**
 * 查询文章管理列表
 */
Article.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    Article.table.refresh({query: queryData});
};

/**
 * 关闭此对话框
 */
Article.close = function () {
    parent.layer.close(window.parent.Article.layerIndex);
};


/**
 * 置顶文章
 */
Article.top = function (top,id) {
    Article.cancelClick(id);
    var ids = new Array();
    var ajax = new $ax(Feng.ctxPath + "/article/article/top", function (data) {
        Feng.pass(data,function () {
            Feng.success("置顶成功!");
            Article.table.refresh();
        });
    }, function (data) {
        Feng.error("置顶失败!" + data.msg + "!");
    },{"traditional":true});
    ids.push(id);
    ajax.set("ids", ids);
    ajax.set("top", top);
    ajax.start();

};


/**
 * 推荐文章
 */
Article.recommend = function (recommend,id) {
    Article.cancelClick(id);
    var ids = new Array();
    var ajax = new $ax(Feng.ctxPath + "/article/article/recommend", function (data) {
        Feng.pass(data,function () {
            Feng.success("推荐成功!");
            Article.table.refresh();
        });
    }, function (data) {
        Feng.error("推荐失败!" + data.msg + "!");
    },{"traditional":true});
    ids.push(id);
    ajax.set("ids", ids);
    ajax.set("recommend", recommend);
    ajax.start();
};

/**
 * 发布文章
 */
Article.punishArticle = function () {
    if (this.check()) {
        var articleId = Article.seItemId;
        var ajax = new $ax(Feng.ctxPath + "/article/article/punishArticle", function (data) {
            Feng.pass(data,function () {
                Feng.success("发布成功!");
                Article.table.refresh();
            });
        }, function (data) {
            Feng.error("发布失败!" + data.msg + "!");
        },{"traditional":true});
        ajax.set("ids", articleId);
        ajax.start();
    }
};



$(function () {
    var defaultColunms = Article.initColumn();
    var table = new BSTable(Article.id, "/article/article/list", defaultColunms);
    Article.table = table.init();
});
